//
//  WMParkingViewController.m
//  witMorning
//
//  Created by Mac mini on 2018/1/8.
//  Copyright © 2018年 zt. All rights reserved.
//

#import "WMParkingViewController.h"
#import "WMParkWebViewController.h"
#import "WMParkInfoViewController.h"
#import "NPHomeHeaderBar.h"

#define head_height 40
#define head_dateHeight 50

@interface WMParkingViewController ()<homeHeaderBarDelegate,UIScrollViewDelegate>
@end

@implementation WMParkingViewController
{
    NPHomeHeaderBar *headerBar;
    UIScrollView *mainScroll;
    WMParkWebViewController *webcontroller;
    WMParkInfoViewController *infocontroller;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self createBar];
    [self createMainScroll];
    
    [self requestData];
}

-(void)requestData{
        NSString *urlStr = [BASE_HTTP stringByAppendingString:@"getParkList"];
        [[WMManage shareInstance] getRequestWithUrl:urlStr success:^(id responseObj) {
            
            NSArray *array = responseObj[@"result"];
            
            webcontroller.obj = responseObj;
            infocontroller.dataArray = array;
            
            
            NSLog(@"%@",responseObj);
            
        } fail:^(id error) {
            
        }];

}


-(void)createBar{
    headerBar= [[NPHomeHeaderBar alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_W, head_height) andTitleArray:@[@"地图概况",@"空位详情"]];
    headerBar.barDelegate = self;
    [self.view addSubview:headerBar];
}


-(void)createMainScroll{
    NSArray<UIView *> *viewArr = @[self.webcontroller.view,self.infocontroller.view];
    
    mainScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,head_height+NAV_HEIGHT,SCREEN_W,CGRectGetHeight(self.view.frame)-head_height-NAV_HEIGHT)];
    mainScroll.delegate = self;
    [self.view addSubview:mainScroll];
    
    for (int i = 0; i<viewArr.count; i++) {
        UIView *view = viewArr[i];
        [mainScroll addSubview:view];
    }
    mainScroll.pagingEnabled = YES;
    [mainScroll setContentSize:CGSizeMake(2*SCREEN_W, 0)];
    
}

-(WMParkWebViewController *)webcontroller{
    if (!webcontroller) {
        webcontroller = [[WMParkWebViewController alloc]init];
        webcontroller.view.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H-NAV_HEIGHT-head_height);

    }
    return webcontroller;
}

-(WMParkInfoViewController *)infocontroller{
    if (!infocontroller) {
        infocontroller = [WMParkInfoViewController alloc];
        infocontroller.view.frame = CGRectMake(SCREEN_W, 0, SCREEN_W, SCREEN_H-NAV_HEIGHT-head_height);
    }
    return infocontroller;
}



#pragma mark ----homeHeaderBarDelegate--点击----
-(void)homeHeaderBarClickWithIndex:(NSInteger)index{
    [mainScroll setContentOffset:CGPointMake(SCREEN_W*index, 0)];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offset = mainScroll.contentOffset.x/(SCREEN_W*2);
    [headerBar setScrollLabWithX:offset];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger page = mainScroll.contentOffset.x/SCREEN_W;
    headerBar.selectButton.selected = NO;
    headerBar.buttonArray[page].selected = YES;
    headerBar.selectButton = headerBar.buttonArray[page];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
